//
//  myVIew.m
//  CarHome
//
//  Created by 苗钧豪 on 16/8/9.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "myVIew.h"
#import "CollectionViewCell.h"
#import "HotCar.h"
@implementation myVIew
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self getup];
        self.hotLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 30)];
        _hotLabel.text = @"热门精选";
        [self addSubview:_hotLabel];
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        self.cview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30, self.bounds.size.width, self.bounds.size.height - 30) collectionViewLayout:layout];
        layout.itemSize = CGSizeMake((self.bounds.size.width - 60) / 5, (self.bounds.size.height - 60) / 2);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        _cview.delegate = self;
        _cview.dataSource = self;
        _cview.backgroundColor = [UIColor whiteColor];
        [_cview registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"2"];
        [self addSubview:_cview];
    }
    return self;
}
- (void)getup {
    self.Array = [NSMutableArray array];
    NSString *HotJSON = [[NSBundle mainBundle] pathForResource:@"Hot" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:HotJSON];
    NSDictionary *Dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
    NSArray *arr = Dic[@"result"][@"list"];
    for (NSDictionary *dic in arr) {
        HotCar *car = [HotCar hotCarWithDic:dic];
        [_Array addObject:car];
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _Array.count;
}
- (__kindof CollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"2" forIndexPath:indexPath];
    cell.hotcar = _Array[indexPath.row];
    return cell;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
